package com.ng.sys.service.impl;

import java.util.List;
import org.springframework.stereotype.Service;
import com.jfinal.plugin.activerecord.Db;
import com.ng.sys.model.domain.*;
import com.ng.sys.service.IDept;

@Service
public class DeptImpl implements IDept {


	@Override
    public List<Dept> listDeptOptions() {
        return Dept.dao.find("select * from "+Dept.TABLE_NAME+" where status='1' order by sort asc");
    }
    
	@Override
	public boolean delete(String ids) {
		String[] arr=ids.split(",");
		if(ids==null || ids.length()<=0) {
			return false;
		}
		for(String id:arr) {
			deleteChildrens(id);
			Db.delete("delete from "+Dept.TABLE_NAME+" where id=?",id);
		}
        return true;
	}
	
	private void deleteChildrens(String id) {
		List<Menu> menus=Menu.dao.find("select id from "+Dept.TABLE_NAME+" where parent_id=?",id);
		for(Menu menu:menus) {
			deleteChildrens(menu.getId());
			menu.delete();
		}
	}

}
